package com.wonders.bigdata.nio;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.nio.channels.FileChannel;

/**
 * Description: 从一个通道copy一张图片<br>
 *
 * @author: XB
 * @date: 2021/5/8 13:52
 */
public class NIOFilechannel04 {
    public static void main(String[] args) throws Exception {
        // 创建相关流
        FileInputStream fileInputStream = new FileInputStream("/file/logo.png");
        FileOutputStream fileOutputStream = new FileOutputStream("/file/netty/logo.png");

        // 使用各个流对应的filechannel
        FileChannel sourceCh = fileInputStream.getChannel();
        FileChannel targetCh = fileOutputStream.getChannel();

        // 使用transferFrom完成copy
        targetCh.transferFrom(sourceCh, 0, sourceCh.size());

        // 关闭相关通道和流
        sourceCh.close();
        targetCh.close();
        fileInputStream.close();
        fileOutputStream.close();
    }
}
